<?php

declare(strict_types=1);

namespace App\Middleware;

use App\Constants\ContextKey;
use App\Constants\ErrorCode;
use App\Exception\BusinessException;
use App\Model\AdminUser;
use App\Model\CooperativeUser;
use App\Model\User;
use App\Utils\Jwt;
use Hyperf\Utils\Context;
use Psr\Container\ContainerInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;

class ImAuthMiddleware implements MiddlewareInterface
{
    /**
     * @var ContainerInterface
     */
    protected $container;

    public function __construct(ContainerInterface $container)
    {
        $this->container = $container;
    }

    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
    {
        if (!$request->hasHeader('authorization')) {
            throw new BusinessException(ErrorCode::UNAUTHORIZED);
        }
        if (!$request->hasHeader('send-platform')){
            throw new BusinessException(ErrorCode::SEND_PLATFORM);
        }
        $token = $request->getHeaderLine('authorization');
        $send = $request->getHeaderLine('send-platform');

        //悬赏主切换平台时使用
        $coopers = $request->getHeaderLine('cooperative_id');

        switch ($send){
            case \App\Constants\ImChat::USER:
                $verifiedToken = Jwt::verify($token);
                $user_id = $verifiedToken->getClaim('user_id');
                $userInfo = User::query()
                    ->with('employer')
                    ->with('info')
                    ->where(['id'=>$user_id])
                    ->first();
                if (!$userInfo) {
                    throw new BusinessException(ErrorCode::USER_NOT_EXIST);
                }
                if ($userInfo->status != 1) {
                    throw new BusinessException(ErrorCode::USER_DISABLE);
                }
                break;
            case \App\Constants\ImChat::PARTNER:
                $verifiedToken = Jwt::verify($token,'jwt.cooperative');
                $cooperativeUserId = $verifiedToken->getClaim('id');
                $userInfo = CooperativeUser::query()->where('id', $cooperativeUserId)->first();
                if (null == $userInfo) {
                    throw new BusinessException(ErrorCode::COOPERATIVE_USER_NOT_FOUND);
                }
                break;
            default:
                $verifiedToken = Jwt::verify($token);
                $adminUserId = $verifiedToken->getClaim('id');
                $userInfo = AdminUser::query()->where('id', $adminUserId)->first();
                if (null == $userInfo) {
                    throw new BusinessException(ErrorCode::ADMIN_USER_NOT_FOUND);
                }
                break;
        }
        $userInfo->prefix = $send.'_';
        if (!empty($coopers)){
            $userInfo->cooperative_employer_user = $coopers;
        }
        Context::set(ContextKey::USE_IM_USER, $userInfo);
        return $handler->handle($request);
    }
}